package com.freshmarket.freshmarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.freshmarket.freshmarket.controller.form.ShopCarForm;
import com.freshmarket.freshmarket.controller.result.ShoppingCartResult;
import com.freshmarket.freshmarket.controller.result.ShoppingCartUserCouponsResult;
import com.freshmarket.freshmarket.entity.ShoppingCar;
import com.freshmarket.freshmarket.service.IShoppingCarService;
import com.freshmarket.freshmarket.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-07
 */
@RestController
@Slf4j
@RequestMapping("/freshmarket/shopping-car")
public class ShoppingCarController {
    @Autowired
    private IShoppingCarService iShoppingCarService;

    @GetMapping("getShoppingCart")
    public Result getShoppingCart(Integer userId){
        List<ShoppingCartResult> shoppingCart = iShoppingCarService.getShoppingCart(userId);
        return Result.successResult(shoppingCart);
    }
    @RequestMapping("addCar")
    public Result addCar( ShopCarForm shopCarForm){
//        log.info("穿的是：{}"+shopCarForm);

        if(shopCarForm.getUserId()==null || shopCarForm.getUserId().equals("")){
            return Result.failedResult(0000,"登录后再添加购物车");
        }
        //修改至service
        System.out.println(shopCarForm);
        ShoppingCar shoppingCar = new ShoppingCar();
        BeanUtils.copyProperties(shopCarForm,shoppingCar);

        iShoppingCarService.addCar(shoppingCar);
        return Result.successResult();
    }
    @PostMapping("updateShoppingCart")
    public Result updateShoppingCart(Integer userId,Integer productId,Integer productNum){
        List<ShoppingCartResult> shoppingCart = iShoppingCarService.updateShoppingCart(userId,productId,productNum);

        return Result.successResult(shoppingCart);
    }
    @GetMapping("getCoupons")
    public Result getCoupons(Integer userId){
        List<ShoppingCartUserCouponsResult> coupons = iShoppingCarService.getCoupons(userId);
        return Result.successResult(coupons);
    }
}

